package com.winsdom.controller;

import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.domain.dto.collection.NotificationDefaultConfigDTO;
import com.winsdom.domain.vo.collection.CustomerBillVO;
import com.winsdom.enums.AuthenticationType;
import com.winsdom.exception.IllegalArgumentException;
import com.winsdom.result.PageResult;
import com.winsdom.result.Result;
import com.winsdom.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/collection")
@Api(tags = "催收管理", description = "催收管理相关接口")
@Authentication(value = {AuthenticationType.NOTIFICATION_TEMPLATE})
public class CollectionController {
    @Resource
    private CollectionService collectionService;

    @OperationLog(description = "查询客户账单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "arrearsStatus", value = "欠费状态", required = true, dataType = "String")
    })
    @GetMapping("/billDetail")
    @ApiOperation(value = "查询客户账单信息")
    public Result<CustomerBillVO> getCustomerBillByCustomerId(
            @RequestParam Long customerId,
            @RequestParam String arrearsStatus) {
        if (customerId == null) {
            throw new IllegalArgumentException("客户ID不能为空");
        }
        if (arrearsStatus == null || (!"1".equals(arrearsStatus) && !"2".equals(arrearsStatus))) {
            throw new IllegalArgumentException("无效的欠费状态，仅支持1（未结算）和2（已结算）");
        }
        return collectionService.getCustomerBillByCustomerId(customerId, arrearsStatus);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "arrearsStatus", value = "欠费状态，1-未结算，2-已结算", required = true, dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "building", value = "楼盘名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int")
    })
    @GetMapping("/bills")
    @OperationLog(description = "查询订单列表")
    @ApiOperation(value = "查询订单列表")
    public PageResult getBills(
            @RequestParam Long userId,
            @RequestParam String arrearsStatus,
            @RequestParam(required = false) String customerName,
            @RequestParam(required = false) String building,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize
    ) {
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        if (arrearsStatus == null || (!"1".equals(arrearsStatus) && !"2".equals(arrearsStatus))) {
            throw new IllegalArgumentException("无效的欠费状态，仅支持1（未结算）和2（已结算）");
        }
        if (pageNum < 1) {
            throw new IllegalArgumentException("页码不能小于1");
        }
        if (pageSize < 1) {
            throw new IllegalArgumentException("每页记录数不能小于1");
        }
        return collectionService.getBillList(userId, arrearsStatus, customerName, building, pageNum, pageSize);
    }

    @PostMapping("/setSmsDefault")
    @ApiOperation("设置客户通知默认配置")
    public Result<Void> setDefaultConfig(@RequestBody NotificationDefaultConfigDTO configDTO) {
        if (configDTO == null) {
            throw new IllegalArgumentException("通知默认配置不能为空");
        }
        collectionService.saveDefaultConfig(configDTO);
        return Result.success();
    }

    @GetMapping("/getSmsDefault")
    @ApiOperation("获取客户通知默认配置")
    public Result<NotificationDefaultConfigDTO> getDefaultConfig() {
        return Result.success(collectionService.getDefaultConfig());
    }

    @GetMapping("/currentUserId")
    @ApiOperation(value = "获取当前登录用户ID", notes = "需先完成登录")
    @OperationLog(description = "查询当前登录用户ID")
    public Result<Integer> getCurrentUserId() {
        Integer userId = collectionService.getCurrentUserId();
        if (userId == null) {
            return Result.error("未检测到登录用户"); // 根据项目错误处理规范调整
        }
        return Result.success(userId);
    }


}